package com.spark.market.service.manager;

import com.spark.market.api.dto.SequenceInfo;
import com.spark.market.common.utils.AssertUtils;
import com.spark.market.dal.entity.SequenceInfoDo;
import com.spark.market.dal.model.SequenceModel;
import com.spark.market.dal.repository.SequenceInfoRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SequenceManager {
    @Resource
    SequenceInfoRepository sequenceInfoRepository;

    public SequenceModel querySequenceInfo(String name) {
        AssertUtils.isNotNull(name, "name is null");
        SequenceInfoDo sequenceInfoDo = sequenceInfoRepository.getSequenceByName(name);
        return convertFromSequenceInfoDo(sequenceInfoDo);
    }

    public void updateSequence(SequenceInfo sequenceInfo) {
        AssertUtils.isNotNull(sequenceInfo, "sequenceInfo is null");
        sequenceInfoRepository.updateSequenceValue(sequenceInfo.getName(), sequenceInfo.getCurrentValue());
    }

    public SequenceModel convertFromSequenceInfoDo(SequenceInfoDo sequenceInfoDo){
        if (sequenceInfoDo == null){
            return null;
        }
        SequenceModel sequenceModel = new SequenceModel();
        BeanUtils.copyProperties(sequenceInfoDo, sequenceModel);
        return sequenceModel;
    }

}
